<?php
/**
 * @see ZendX_JQuery_View_Helper_UiWidget
 */
require_once "ZendX/JQuery/View/Helper/UiWidget.php";

/**
 * Помощник вида, реализующий добавление множественных полей (например, для списка тегов)
 * Предназначен для того, чтобы его расширять (дабы не повторять одни и теже операции)
 * 
 * @category Phorm
 * @package JQuery_View_Helper
 * @name /Phorm/JQuery/View/Helper/Multi.php
 * @copyright phorm.ru
 * @since 29.01.2011
 * @author vladimir@kachalov.net
 */

class Phorm_JQuery_View_Helper_Multi extends ZendX_JQuery_View_Helper_UiWidget {
	
	protected $_buttonid;
	
	protected $_name;
	
	protected $_id;
	
	protected $_ulid;
	
	protected $_translate;
	
	protected $_class = 'elementMultiFields';
	
	protected $_deleteimg;
	
	protected $_options = array();
	
	protected $_params = array();
	
	protected $_attribs = array();
	
	protected $_elementattribs = array();
	
	/**
	 * Добавляет JS-коды на элементы
	 *
	 * @param string $id Id HTML-элемента
	 * @param string $value Значение элемента
	 * @param array $params Массив параметров элемента
	 * @param array $attribs Массив атрибутов HTML-элемента
	 * @return self
	 */
	
	protected function initElement($id, $value = null, $params = array(), $attribs = array()) {
		
		if(isset($params['class'])) $attribs['class'] = $params['class'];
		if(isset($params['style'])) $attribs['style'] = $params['style'];
		
		$this->_translate = $this->view->translate()->getTranslator();
		$this->_name = preg_replace("#\[\]$#","",$id);
		$this->_id = $id;
		$this->_buttonid = $this->_name . '_AddField';
		$this->_ulid = $this->_name . '_' . $this->_class;
		if(isset($params['elementoptions'])) {
			$this->_elementattribs = $params['elementoptions'];
			unset($params['elementoptions']);
		}
		$this->_params = $params;
		$this->_attribs = $attribs;
		
		$this->_options = isset($params['options']) ? $params['options'] : array();
        if(is_array($value)) $this->_options += $value;
		
		$this->_params['button'] = isset($this->_params['button']) ? $this->_translate->_($this->_params['button']) : $this->_translate->_('Добавить');
		$this->_params['delete'] = isset($this->_params['delete']) ? $this->_translate->_($this->_params['delete']) : $this->_translate->_('Удалить');
		
		$this->_deleteimg = '<a href="#" class="delete ui-corner-all ui-button ui-state-default" title="'.$this->view->escape($this->_params['delete']).'">
								<span class="ui-icon ui-icon-close"></span>
							 </a>';
		
		return $this->initJavaScript();
		
	}
	
	
	/**
	 * Добавляет JS-код управления элементом
	 *
	 * @return self
	 */
	
	protected function initJavaScript() {
		
		$jquery_handler = ZendX_JQuery_View_Helper_JQuery::getJQueryHandler();
		
		if(isset($this->_params['sortable']) && $this->_params['sortable']!=0) {
			$js = sprintf('%s("#%s").sortable().find("li").css({cursor:"move"});', $jquery_handler, $this->_ulid);
			$this->jquery->addOnLoad($js);
		}

        $js = sprintf('%s("#%s").click(function(){ 
	        		var src = %s(this).next("ul").find("li:last"); var trgt = src.clone().insertBefore(src).show();
					trgt.find(":disabled").attr("disabled",false);
					trgt.find(".delete").click(function(){ 
						if(confirm("'.$this->view->escape($this->_params['delete']).'?")) %s(this).parent("li").remove();
						return false; 
					});
					return false;
	        	});',
        	$jquery_handler, $this->_buttonid, $jquery_handler, $jquery_handler
        );

        $this->jquery->addOnLoad($js);
		
		$js = sprintf('%s("#%s .delete").click(function(){ 
					if(confirm("'.$this->view->escape($this->_params['delete']).'?")) { 
						%s(this).parent("li").remove(); 
					} return false;
				});',
			$jquery_handler,$this->_ulid, $jquery_handler);

        $this->jquery->addOnLoad($js);
        
        return $this;
		
	}
	
}